﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Aij1.Model;
using DevExpress.Xpo;
using DevExpress.Data.Filtering;
using System.Data;
using Aij1.Utils;

namespace Aij1.BLL
{
    public class SeriesData
    {
        public static B_Series CreateNewSeries(Session session)
        {
            S_Person user = AuthData.GetCurrentUser(session);
            B_Series art = new B_Series(session)
            {
                DisplayTime = DateTime.Now,
                EditorId = user,
                Author = user == null ? "" : user.TrueName,
            };
            return art;
        }

        public static SortProperty[] GetDefaultSort()
        {
            return new SortProperty[]{
                new SortProperty("SetTopTime", DevExpress.Xpo.DB.SortingDirection.Descending),
                new SortProperty("DisplayTime", DevExpress.Xpo.DB.SortingDirection.Descending),
            };
        }

        public static B_Series GetNextNews(B_Series News)
        {
            XPCollection<B_Series> arts = new XPCollection<B_Series>(News.Session,
                CriteriaOperator.Parse("Oid<? AND CategoryId=?", News.Oid, News.CategoryId),
                new SortProperty("Oid", DevExpress.Xpo.DB.SortingDirection.Descending));
            arts.TopReturnedObjects = 1;
            return arts.Count == 0 ? null : arts[0];
        }

        public static B_Series GetPrevNews(B_Series News)
        {
            XPCollection<B_Series> arts = new XPCollection<B_Series>(News.Session,
                CriteriaOperator.Parse("Oid>? AND CategoryId=?", News.Oid, News.CategoryId),
                new SortProperty("Oid", DevExpress.Xpo.DB.SortingDirection.Ascending));
            arts.TopReturnedObjects = 1;
            return arts.Count == 0 ? null : arts[0];
        }

        public static IDataReader GetPagedNewsList(DbPager pager)
        {
            return DBHelper.DefaultHelper.ExecPageReader(pager);
        }

        public static XPCollection<B_Series> GetTopNews(Session session, string catIds, int topN)
        {
            XPCollection<B_Series> arts = new XPCollection<B_Series>(session,
                CriteriaOperator.Parse(String.Format("CategoryId IN ({0})", catIds)));
            arts.Sorting.AddRange(GetDefaultSort());

            arts.TopReturnedObjects = topN;
            return arts;
        }

        public static string GetTopNewsHtml(Session session, string catIds, int topN)
        {
            string format = @"<li><span class='orange'>·</span>
                <a href='{3}?Id={0}' title='{1}' target='_blank'>{1}</a> <span class='ind_time'>{2:MM-dd}</span></li>";

            StringBuilder sb = new StringBuilder();

            foreach (B_Series art in GetTopNews(session, catIds, topN))
            {
                sb.AppendFormat(format, art.Oid, art.Title, art.DisplayTime, 
                    art.AType == ArticleType.新闻 ? "News.aspx" : "Gallery.aspx");
            }
            return sb.ToString();
        }

        public static void SetTop(Session session, string ids)
        {
            string sql = String.Format("UPDATE B_Series SET SetTopTime='{0}' WHERE Oid IN ({1})", DateTime.Now, ids);
            session.ExecuteNonQuery(sql);
        }

        public static void UnsetTop(Session session, string ids)
        {
            string sql = String.Format("UPDATE B_Series SET SetTopTime=NULL WHERE Oid IN ({0})", ids);
            session.ExecuteNonQuery(sql);
        }

        public static void Delete(Session session, string ids)
        {
            string sql = String.Format("DELETE FROM B_Series WHERE Oid IN ({0})", ids);
            session.ExecuteNonQuery(sql);

        }
    }
}
